package com.liangchen.astools.info

import android.app.ActivityManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.wifi.WifiManager
import android.os.Build
import com.liangchen.astools.R
import java.util.*

class AsInfoManager(private val mContext: Context) {
    private val infoItemList: MutableList<InfoItem> = ArrayList()
    fun initInfoItems(): List<InfoItem> {
        infoItemList.clear()
        // App Info
        var item = InfoItem("应用信息", true)
        infoItemList.add(item)
        for (tag in TAG_APP_INFO_LIST) {
            item = InfoItem(getNameStrByTag(tag), getValueStrByTag(tag))
            infoItemList.add(item)
        }
        item = InfoItem("设备信息", true)
        infoItemList.add(item)
        for (tag in TAG_DEVICE_INFO_LIST) {
            item = InfoItem(getNameStrByTag(tag), getValueStrByTag(tag))
            infoItemList.add(item)
        }
        return infoItemList
    }

    //返回版本号
    //获取应用 信息
    private val appName:
    //获取albelRes
    //返回App的名称
            String?
        private get() {
            val pm = mContext.packageManager
            try {
                val packageInfo = pm.getPackageInfo(mContext.packageName, 0)
                //返回版本号
                //获取应用 信息
                val applicationInfo = packageInfo.applicationInfo
                //获取albelRes
                val labelRes = applicationInfo.labelRes
                //返回App的名称
                return mContext.resources.getString(labelRes)
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }
    val TAG_APP_INFO_LIST = arrayOf(
        "TAG_APP_NAME",
        "TAG_APP_VERSION_CODE",
        "TAG_APP_DEBUGGABLE",
        "TAG_APP_BUILD_TYPE",
        "TAG_APP_COMPILE_SDK",
        "TAG_APP_INSTALL_PATH",
        "TAG_APP_UPDATE_TIME",
        "TAG_APP_CACHE_SIZE",
        "TAG_APP_APK_SIZE",
        "TAG_APP_HEAP_SIZE",
        "TAG_APP_HEAP_GROWTH_LIMIT"
    )
    val TAG_DEVICE_INFO_LIST = arrayOf(
        "TAG_DEVICE_NAME",
        "TAG_DEVICE_BRAND",
        "TAG_DEVICE_MANUFACTURER",
        "TAG_DEVICE_ANDROID_VERSION",
        "TAG_DEVICE_ROM_BUILD_FINGERPRINT",
        "TAG_DEVICE_WIFI_IP_ADDRESS"
    )

    private fun getNameStrByTag(tagName: String): String {
        var nameStrId = 0
        nameStrId = when (tagName) {
            "TAG_APP_NAME" -> R.string.tag_name_app_name
            "TAG_APP_VERSION_CODE" -> R.string.tag_name_app_version_code
            "TAG_APP_DEBUGGABLE" -> R.string.tag_name_app_debuggable
            "TAG_APP_BUILD_TYPE" -> R.string.tag_name_app_build_type
            "TAG_APP_COMPILE_SDK" -> R.string.tag_name_app_compile_sdk
            "TAG_APP_INSTALL_PATH" -> R.string.tag_name_app_install_path
            "TAG_APP_UPDATE_TIME" -> R.string.tag_name_app_update_time
            "TAG_APP_CACHE_SIZE" -> R.string.tag_name_app_cache_size
            "TAG_APP_APK_SIZE" -> R.string.tag_name_app_apk_size
            "TAG_APP_HEAP_SIZE" -> R.string.tag_name_app_heap_size
            "TAG_APP_HEAP_GROWTH_LIMIT" -> R.string.tag_name_app_heap_growth_limit
            "TAG_DEVICE_NAME" -> R.string.tag_name_device_name
            "TAG_DEVICE_BRAND" -> R.string.tag_name_device_brand
            "TAG_DEVICE_MANUFACTURER" -> R.string.tag_name_device_manufacturer
            "TAG_DEVICE_ANDROID_VERSION" -> R.string.tag_name_device_android_version
            "TAG_DEVICE_ROM_BUILD_FINGERPRINT" -> R.string.tag_name_device_rom_build_fingerprint
            "TAG_DEVICE_WIFI_IP_ADDRESS" -> R.string.tag_name_device_wifi_address
            else -> R.string.tag_name_unknown
        }
        return mContext.getString(nameStrId)
    }

    private fun getValueStrByTag(tagName: String): String? {
        var resultValue: String? = null
        resultValue = when (tagName) {
            "TAG_APP_NAME" -> appName
            "TAG_APP_VERSION_CODE" -> appVersion
            "TAG_APP_DEBUGGABLE" -> isAppDebuggable
            "TAG_APP_BUILD_TYPE" -> appBuildType
            "TAG_APP_COMPILE_SDK" -> appCompileSdk.toString() + ""
            "TAG_APP_INSTALL_PATH" -> appPackagePath
            "TAG_APP_UPDATE_TIME" -> appUpdateTime
            "TAG_APP_CACHE_SIZE" -> appCacheSize.toString() + ""
            "TAG_APP_APK_SIZE" -> appApkSize.toString() + ""
            "TAG_APP_HEAP_SIZE" -> appHeapSizeMb.toString() + ""
            "TAG_APP_HEAP_GROWTH_LIMIT" -> appGrowthLimit.toString() + ""
            "TAG_DEVICE_NAME" -> deviceName
            "TAG_DEVICE_BRAND" -> deviceBrand
            "TAG_DEVICE_MANUFACTURER" -> deviceManufacturer
            "TAG_DEVICE_ANDROID_VERSION" -> deviceAndroidVersion
            "TAG_DEVICE_ROM_BUILD_FINGERPRINT" -> deviceRomBuildFingerprint
            "TAG_DEVICE_WIFI_IP_ADDRESS" -> deviceWifiIpAddr
            else -> "unknown"
        }
        return resultValue
    }

    private val deviceName: String
        private get() = Build.DEVICE
    private val appGrowthLimit: Int
        private get() {
            val activityManager =
                mContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            return activityManager.memoryClass
        }
    private val appHeapSizeMb: Int
        private get() {
            val activityManager =
                mContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            return activityManager.largeMemoryClass
        }
    private val appCacheSize: Long
        private get() = 0
    private val appBuildType: String?
        private get() = null

    /**
     * 获取手机厂商
     *
     * @return 手机厂商
     */
    val deviceBrand: String
        get() = Build.BRAND
    private val deviceAndroidVersion: String
        private get() = Build.VERSION.RELEASE
    private val deviceRomBuildFingerprint: String
        private get() = Build.FINGERPRINT
    //获取WifiManager
    //检查wifi是否开启

    //wifiinfo.getIpAddress()

    //将获取的int转为真正的ip地址,参考的网上的，修改了下
    private val deviceWifiIpAddr: String?
        private get() {
            var ip: String? = null
            val wifimanage =
                mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager //获取WifiManager
            //检查wifi是否开启
            if (!wifimanage.isWifiEnabled) {
                wifimanage.isWifiEnabled = true
            }
            val wifiinfo = wifimanage.connectionInfo

            //wifiinfo.getIpAddress()
            ip = intToIp(wifiinfo.ipAddress)

//将获取的int转为真正的ip地址,参考的网上的，修改了下
            return ip
        }

    private fun intToIp(i: Int): String {
        return (i and 0xFF).toString() + "." +
                (i shr 8 and 0xFF) + "." +
                (i shr 16 and 0xFF) + "." +
                (i shr 24 and 0xFF)
    }

    private fun getDesciptionByTag(tagName: String): String {
        var descriptionStrId = 0
        when (tagName) {
            "TAG_APP_NAME" -> descriptionStrId = R.string.tag_description_app_name
            "TAG_APP_VERSION_CODE" -> descriptionStrId = R.string.tag_description_app_version_code
            "TAG_APP_DEBUGGABLE" -> descriptionStrId = R.string.tag_description_app_debuggable
            "TAG_APP_BUILD_TYPE" -> descriptionStrId = R.string.tag_description_app_build_type
            "TAG_APP_COMPILE_SDK" -> descriptionStrId = R.string.tag_description_app_compile_sdk
            "TAG_APP_INSTALL_PATH" -> descriptionStrId = R.string.tag_description_app_install_path
            "TAG_APP_UPDATE_TIME" -> descriptionStrId = R.string.tag_description_app_update_time
            "TAG_APP_CACHE_SIZE" -> descriptionStrId = R.string.tag_description_app_cache_size
            "TAG_APP_APK_SIZE" -> descriptionStrId = R.string.tag_description_app_apk_size
            "TAG_APP_HEAP_SIZE" -> descriptionStrId = R.string.tag_description_app_heap_size
            "TAG_APP_HEAP_GROWTH_LIMIT" -> descriptionStrId =
                R.string.tag_description_app_heap_growth_limit
            "TAG_DEVICE_NAME" -> descriptionStrId = R.string.tag_description_device_name
            "TAG_DEVICE_BRAND" -> descriptionStrId = R.string.tag_description_device_brand
            "TAG_DEVICE_MANUFACTURER" -> {
                descriptionStrId = R.string.tag_description_device_manufacturer
                descriptionStrId = R.string.tag_description_device_android_version
            }
            "TAG_DEVICE_ANDROID_VERSION" -> descriptionStrId =
                R.string.tag_description_device_android_version
            "TAG_DEVICE_ROM_BUILD_FINGERPRINT" -> descriptionStrId =
                R.string.tag_description_device_rom_build_fingerprint
            "TAG_DEVICE_WIFI_IP_ADDRESS" -> descriptionStrId =
                R.string.tag_description_app_device_wifi_address
            else -> descriptionStrId = R.string.tag_description_unknown
        }
        return mContext.getString(descriptionStrId)
    }

    //返回版本号
    private val appVersion: String?
        private get() {
            val pm = mContext.packageManager
            try {
                val packageInfo = pm.getPackageInfo(mContext.packageName, 0)
                //返回版本号
                return packageInfo.versionName
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }
    private val appPackagePath: String
        private get() = mContext.packageResourcePath

    //返回版本号
    private val appUpdateTime: String?
        private get() {
            val pm = mContext.packageManager
            try {
                val packageInfo = pm.getPackageInfo(mContext.packageName, 0)
                //返回版本号
                return "install:" + packageInfo.firstInstallTime + "/update:" + packageInfo.lastUpdateTime
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }
    private val isAppDebuggable: String
        private get() {
            val isDebuggable =
                0 != mContext.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
            return if (isDebuggable) {
                "True"
            } else {
                "false"
            }
        }
    private val appCompileSdk: Int
        private get() {
            val pm = mContext.packageManager
            try {
                val packageInfo = pm.getPackageInfo(mContext.packageName, 0)
                return 29
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return -1
        }
    private val appApkSize: Long
        private get() = 50
    private val appDataSize: Long
        private get() = 100

    class InfoItem {
        constructor(name: String, value: String?) {
            itemName = name
            itemValue = value
        }

        constructor(title: String, isTitle: Boolean) {
            itemName = title
            this.isTitle = isTitle
        }

        var itemName: String
        var itemValue: String? = null
        var isTitle = false
    }

    companion object {
        /**
         * 获取手机型号
         *
         * @return 手机型号
         */
        val systemModel: String
            get() = Build.MODEL

        /**
         * 获取手机设备名
         *
         * @return 手机设备名
         */
        val systemDevice: String
            get() = Build.DEVICE

        /**
         * 获取手机主板名
         *
         * @return 主板名
         */
        val deviceBoand: String
            get() = Build.BOARD

        /**
         * 获取手机厂商名
         *
         * @return 手机厂商名
         */
        val deviceManufacturer: String
            get() = Build.MANUFACTURER
    }
}